﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using KLTP2UI.Properties;
using System.Text;

namespace KLTP2UI
{
	public partial class Form1 : Form
	{
		private uint _tIdx;
		private ParseController _controller;
		public StringBuilder buffer = new StringBuilder(4096);

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1Load(object sender, EventArgs e)
		{
			DllMem.MmInitialize();
		}
		
		private void Form1FormClosing(object sender, FormClosingEventArgs e)
		{
			if(_controller != null)
				_controller.StopProcessing();

			DllMem.MmDeInitialize();
		}

		private void Button1Click(object sender, EventArgs e)
		{
			if(_controller!=null && _controller.IsProcessing())
			{
				button1.Enabled = false;
				_controller.StopProcessing();
				return;
			}
				

			if (openFileDialog1.ShowDialog() != DialogResult.OK) 
				return;

			textBox2.Text = openFileDialog1.FileName;
				
			if(_controller != null)
				_controller.StopProcessing();

			listBox1.Items.Clear();
			dataGridView1.RowCount = 0;
			
			button1.Enabled = false;
			DllMem.MmReInitialize();
			button1.Enabled = true;

			_controller = new ParseController(openFileDialog1.FileName);
			_controller.StartProcessing();
			_controller.SomeProgress += ControllerSomeProgress;

			button1.Text = Resources.F1B1Cancel;
			progressBar1.Visible = true;
			progressBar1.Value = 0;
		}

		private void ControllerSomeProgress(object sender, EventArgs e)
		{
			if (InvokeRequired)
			{
				BeginInvoke((EventHandler)ControllerSomeProgress, new []{sender, e});
				return;
			}

			var controller = sender as ParseController;
			if(controller == null)
				return;

			if(controller.IsProcessing())
			{
				progressBar1.Maximum = (int)(Math.Ceiling(controller.GetLength() / 10.0));
				progressBar1.Value = (int)(Math.Ceiling(controller.GetProcessed() / 10.0));
			}
			else
			{
				button1.Text = Resources.F1B1Load;
				button1.Enabled = true;
				for (uint tIdx = 0; tIdx < DllMem.GetThreadsCountTotal(); tIdx++)
					listBox1.Items.Add(DllMem.GetThreadIdForIndex(tIdx).ToString("X"));
				progressBar1.Visible = false;
			}
		}

		private void ListBox1SelectedIndexChanged(object sender, EventArgs e)
		{
			dataGridView1.RowCount = 0;
			_tIdx = (uint)(listBox1.SelectedIndex);
			dataGridView1.RowCount = (int)DllMem.GetThreadEventsCount(_tIdx);
		}

		private void DataGridView1CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
		{
			var column = dataGridView1.Columns[e.ColumnIndex];
			if (object.ReferenceEquals(column, colType))
				e.Value = imageList1.Images[DllMem.GetEventType(_tIdx, (uint)e.RowIndex)];
				//See CMemMan::GetTypeFast() for details
			else if (object.ReferenceEquals(column, colTime))
				e.Value = Marshal.PtrToStringAnsi(DllMem.GetEventTime(_tIdx, (uint)e.RowIndex), 12);
			else if (object.ReferenceEquals(column, colSrv))
				e.Value = Marshal.PtrToStringAnsi(DllMem.GetEventService(_tIdx, (uint)e.RowIndex));
			else if (object.ReferenceEquals(column, colMsg))
			{
				buffer.Clear();
				DllMem.GetEventMessageEx(_tIdx, (uint)e.RowIndex, buffer);
				e.Value = buffer.ToString();
				//e.Value = Marshal.PtrToStringAnsi(DllMem.GetEventMessage(_tIdx, (uint)e.RowIndex));
				//this still works returning raw message (not expanded)
			}
			else
				e.Value = null;
		}
	}
}